package yl.hs.bmipfull.controllers.laboratorydata;

import com.sun.net.httpserver.Authenticator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.laboratorydata.ILabExperimentItemsService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:   <br/>
 * 创建时间: 2022-06-20 11:06 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "实验检测标准——检测项", tags = "实验检测标准——检测项")
@RequestMapping(value = "/api/laboratorydata/Items")
@RestController
public class LabExperimentItemsController extends ControllerBase {
    @Autowired
    ILabExperimentItemsService service;
    @ApiOperation(value = "查询检测项信息", notes = "查询检测项信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询检测项信息", notes = "分页查询检测项信息")
    @RequestMapping(value = {"/{rowNum}/{page}","/{rowNum}/{page}/{productCateSn}"}, method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "保存检测项信息", notes = "查询/更新检测项信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveLabExperimentItems(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "根据标准库编号查询检测项信息",notes = "根据标准库编号查询检测项信息")
    @RequestMapping(value = "/{definitionSN}",method=RequestMethod.GET)
    public WebResult queryByDefinitionSN(@PathVariable String definitionSN){
        try{
            return success("查询成功",service.queryByDefinitionSN(definitionSN));
        }catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "根据标准库编号查询检测项信息",notes = "根据标准库编号查询检测项信息")
    @RequestMapping(value = "/parentsn/{parentSN}",method=RequestMethod.GET)
    public WebResult queryByparentSN(@PathVariable String parentSN){
        try{
            return success("查询成功",service.queryByParentSN(parentSN));
        }catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "根据标准库编号删除检测项信息",notes = "根据标准库编号删除检测项信息")
    @RequestMapping(value = "/delete/{sn}",method=RequestMethod.GET)
    public WebResult deleteBySN(@PathVariable String sn){
        try{
            return success("删除成功",service.deleteBySn(sn));
        }catch (Exception ee){
            return error(ee);
        }
    }
    @ApiOperation(value = "获取检测项信息树形",notes = "获取检测项信息树形")
    @RequestMapping(value = "/gettree",method=RequestMethod.GET)
    public WebResult GetTree(){
        try{
            var result = service.queryList(new PageQuery());
            var data = Tl.toTreeList(result,null);
            return success("查询成功",data);
        }catch (Exception ee){
            return error(ee);
        }
    }
}
